<?php

namespace app\admin\controller\curriculum;

use app\admin\{
    controller\Controllers,
    model\curriculum\SchoolTimetable as SchoolTimetableModel,
    service\curriculum\SchoolTimetable as SchoolTimetableService,
    service\curriculum\Curriculum as CurriculumService
};

class SchoolTimetable extends Controllers
{
    /**
     * 显示资源列表
     *
     * @param SchoolTimetableService $service
     * @return \think\Response
     */
    public function index( SchoolTimetableService $service, CurriculumService $curriculumService )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\curriculum\SchoolTimetable.index');
        $list = $service->indexPaginate($get);
        $curriculum = $curriculumService->getList();
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'curriculum' => $curriculum
        ]);
        return $this->fetch('curriculum/school_timetable/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create( CurriculumService $curriculumService )
    {
        $this->assign([
            'curriculum' => $curriculumService->getList()
        ]);
        return $this->fetch('curriculum/school_timetable/create');
    }

    /**
     * 保存新建的资源
     *
     * @return \think\Response
     */
    public function save( SchoolTimetableService $service )
    {
        $post = $this->request->only(['title','duration','cycle','start_cycle_time','end_cycle_time','live_url','delayed_url','cid'], 'post');
        $this->validate($post, 'app\admin\validate\curriculum\SchoolTimetable.Create');
        $schoolTimetableid = $service->create($post);
        if ( false === $schoolTimetableid ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  SchoolTimetableModel $model
     * @return \think\Response
     */
    public function edit( SchoolTimetableModel $model, CurriculumService $curriculumService )
    {
        $this->assign([
            'curriculum' => $curriculumService->getList(),
            'data' => $model
        ]);
        return $this->fetch('curriculum/school_timetable/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  SchoolTimetableModel $model
     * @param  SchoolTimetableService $service
     * @return \think\Response
     */
    public function update(SchoolTimetableModel $model, SchoolTimetableService $service)
    {
        $put = $this->request->only(['title','duration','cycle','start_cycle_time','end_cycle_time','live_url','delayed_url','cid'], 'put');
        $this->validate($put, 'app\admin\validate\curriculum\SchoolTimetable.Update');
        $update = $service->save($model, $put);
        if ( false === $update ) {
            return failed('编辑成功');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, SchoolTimetableService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\curriculum\SchoolTimetable.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 批量删除
     * @param CurriculumService $service
     * @return \think\Response
     */
    public function destory(SchoolTimetableService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\curriculum\SchoolTimetable.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
